package com.wei.czz.framework.game.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wei.czz.common.dto.game.FlowDto;
import com.wei.czz.framework.admin.service.UserService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wei.czz.framework.game.dao.GamePrizeFlowDao;
import com.wei.czz.framework.game.entity.GamePrizeFlowEntity;
import com.wei.czz.framework.game.service.GamePrizeFlowService;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2023-03-16 11:37:01
 * className: GamePrizeFlowServiceImpl 游戏奖品变更流水请求服务接口实现类
 * version: 1.0
 * description:
 */
@Service("gamePrizeFlowService")
@AllArgsConstructor
public class GamePrizeFlowServiceImpl extends ServiceImpl<GamePrizeFlowDao, GamePrizeFlowEntity> implements GamePrizeFlowService {

    private static final Logger log = LoggerFactory.getLogger(GamePrizeFlowServiceImpl.class);

    private final UserService userService;

    @Override
    public void saveGamePrizeFlow(Long prizeId, String title, String content, Long createUser) {
        GamePrizeFlowEntity gamePrizeFlow = new GamePrizeFlowEntity();
        gamePrizeFlow.setPrizeId(prizeId)
                .setTitle(title)
                .setContent(content)
                .setCreateTime(new Date())
                .setCreateUser(createUser);
        // 插入数据
        baseMapper.insert(gamePrizeFlow);
        log.info("记录奖品流水完成");
    }

    @Override
    public void batchSaveGamePrizeFlow(List<Long> prizeIdList, String title, String content, Long createUser) {
        List<GamePrizeFlowEntity> gamePrizeFlowList = new ArrayList<>(prizeIdList.size());
        Date date = new Date();
        for (Long prizeId : prizeIdList) {
            GamePrizeFlowEntity gamePrizeFlow = new GamePrizeFlowEntity();
            gamePrizeFlow.setPrizeId(prizeId)
                    .setTitle(title)
                    .setContent(content)
                    .setCreateTime(date)
                    .setCreateUser(createUser);
            // 记录数据
            gamePrizeFlowList.add(gamePrizeFlow);
        }
        boolean bool = this.saveBatch(gamePrizeFlowList);
        log.info("批量保存游戏奖品流水数据完成。bool={}", bool);
    }

    @Override
    public List<FlowDto> getPrizeFlowList(Long prizeId) {

        LambdaQueryWrapper<GamePrizeFlowEntity> gamePrizeFlowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 查询字段
        gamePrizeFlowLambdaQueryWrapper.select(GamePrizeFlowEntity::getTitle, GamePrizeFlowEntity::getContent,
                GamePrizeFlowEntity::getCreateTime, GamePrizeFlowEntity::getCreateUser);
        // 匹配条件
        gamePrizeFlowLambdaQueryWrapper.eq(GamePrizeFlowEntity::getPrizeId, prizeId);
        gamePrizeFlowLambdaQueryWrapper.orderByDesc(GamePrizeFlowEntity::getId);
        // 查询数据
        List<GamePrizeFlowEntity> prizeFlowList = baseMapper.selectList(gamePrizeFlowLambdaQueryWrapper);
        // 数据封装
        Set<Long> userIdSet = prizeFlowList.stream().map(GamePrizeFlowEntity::getCreateUser).collect(Collectors.toSet());
        Map<Long, String> userNameMap = userService.getNameMap(userIdSet, Function.identity());

        return prizeFlowList.stream()
                .map(gamePrizeFlow -> {
                    FlowDto flowDto = new FlowDto();
                    flowDto.setTitle(gamePrizeFlow.getTitle())
                            .setContent(gamePrizeFlow.getContent())
                            .setCreateTime(gamePrizeFlow.getCreateTime())
                            .setCreateUser(userNameMap.get(gamePrizeFlow.getCreateUser()));
                    return flowDto;
                })
                .collect(Collectors.toList());
    }
}